<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class modeltransformation extends model
{
	
	function __construct(  )
	{
		
		
		
	}
	
	
	public function getItem( $user_id, $id ){
				
		$result=false;
		
		try{
				 		
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
		
			$query = sprintf("SELECT  id,
										named,
										detail,
										json_rule,
										execute_order,
										date_create,	
										view_id
										
							FROM \"%s%s\" WHERE user_id=%d and id=%d;",
					configuration::table_prefix,
                 	configuration::table_transformation, 
                 	application::getUser_id(), 
					$id);
		

			$result = $db->getQuery($query);
		

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
	public function getList( $user_id, $view_id, $column, $order, $limit=10, $offset=0 ){
				
		$result=false;
		
		try{
				 		
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$pagination = sprintf(" LIMIT %u ",$limit);	
			$pagination = $pagination.sprintf(" OFFSET %u ",$offset);
			
			$query = sprintf("SELECT  id,
										named,
										detail,
										json_rule,
										execute_order,
										date_create,
										date_modified
										
							FROM \"%s%s\" WHERE user_id=%d and view_id=%d
							ORDER BY %s %s
							%s;",
							configuration::table_prefix,
		                 	configuration::table_transformation, 
		                 	application::getUser_id(), 
							$view_id,
							$column,
							$order,
							$pagination);
		

			$result = $db->getQuery($query);
			
			//var_dump( $result );exit();

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}


	
	
	public function save( $parameters ){
				
		$result=false;
				
		try {
        						
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$parameters->json_rule = urldecode($parameters->json_rule);
			
			
			if(empty($parameters->id)){
				
				$query = sprintf("INSERT INTO \"%s%s\" (user_id, 
														json_rule,
														execute_order,
														view_id,
														named,
														detail
														) 
								VALUES (%u,'%s',%u,%u,'%s','%s');",
									configuration::table_prefix,
				                 	configuration::table_transformation, 
				                 	application::getUser_id(),
				                 	$parameters->json_rule,
				                 	$parameters->execute_order,
				                 	$parameters->view_id,
				                 	$parameters->named,
				    				$parameters->detail);
				
			}else{
				
				$query = sprintf("UPDATE \"%s%s\"  SET											
										json_rule='%s',
										execute_order=%u,											
										date_modified=statement_timestamp(),
										named='%s',
										detail='%s'
												
								WHERE id=%u and user_id=%u and view_id=%u",
									configuration::table_prefix,
									configuration::table_transformation, 						
									$parameters->json_rule,
									$parameters->execute_order,	
									$parameters->named,
									$parameters->detail,
									$parameters->id,  
									application::getUser_id(),               	
									$parameters->view_id);

			}
			
		
			try{
				$result = $db->setQuery($query);
			}
			catch (AppException $e) {
		    	$result=false;
		    }


	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;

	}
	
	
	public function delete( $user_id, $id ){
				
		$result=false;
				
		try {
        						
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	

			if(!empty($id)){

				$query = sprintf("DELETE FROM  \"%s%s\" 
								WHERE id=%u and user_id=%u",
								configuration::table_prefix,
			                 	configuration::table_transformation, 
								$id,
								$user_id);

				try{
					
					$result = $db->setQuery($query);
					
				}catch (AppException $e) {
			    	$result=false;
			    }	
		
			}

			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}


	public function cloneItem( $user_id, $id ){
				
		$result=false;
		
		try {
					
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);

			
			if( !empty($id)){
				
			
				$query = sprintf("SELECT  	json_rule,
											execute_order,
											view_id,
											named,
											detail
	  						 FROM \"%s%s\" WHERE user_id=%d and id=%d;",
	  					configuration::table_prefix,
	                 	configuration::table_transformation, 
	                 	application::getUser_id(), 
	                 	$id);
						
				$result = $db->getQuery($query);		
				
				$query = sprintf("INSERT INTO \"%s%s\" (
										user_id, 
										json_rule,  
										execute_order,
										view_id,
										named,
										detail)
							VALUES
										(%u,
										'%s',
										'%s',
										%u,
										'%s',
										'%s')",
								configuration::table_prefix,		
			                 	configuration::table_transformation, 
			                 	application::getUser_id(), 
			                 	$result[0]->json_rule,
			                 	$result[0]->execute_order,
			                 	$result[0]->view_id,
			                 	$result[0]->named,
			                 	$result[0]->detail);
				
				try{
					$result = $db->setQuery($query);
					
				}catch (AppException $e) {
			    	$result=false;//throw new AppException( $e->getMessage() );
			    }
						
				
			}
		
		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }

		return $result;
		
	}
	
	
	
	
	public function executeItem( $user_id, $id ){
				
		$result=false;
		
		try {
					
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			
			if( !empty($id)){
			
				$query = sprintf("SELECT * FROM \"%s%s\" 
									WHERE user_id=%d and id=%d;",
									configuration::table_prefix,
				                 	configuration::table_transformation, 
				                 	application::getUser_id(), 
				                 	$id);
						
				$result = $db->getQuery($query);
				
				
				
				if(count($result)>0){
					
					$rules	=	json_decode($result[0]->json_rule);
					
					if( !empty($rules) ){
						
						
						foreach( $rules as $key=>$item){
						
						//foreach($item as $key2=>$item2){
						//	echo $key2."<br>";
						//}
						//echo $item->rule;
						
							switch($item->rule){
								
								case 'convert':
									
									$query = sprintf("UPDATE \"%s\" SET
													%s=REPLACE(%s,'%s','%s')
													",
												md5($result[0]->view_id), 
							                 	$item->column,
							                 	$item->column,
							                 	$item->from,
							                 	$item->to);
												
									try{
										$result = $db->setQuery($query);
										
									}catch (AppException $e) {
								    	$result=false;
								    }
						
						
									break;
								default:
										
										
									break;
							}
							
						}
							
						
					}
					
						
					
				}
				
			}
		
		}
	    catch (AppException $e) {
	    	
			throw new AppException( $e->getMessage() );
			
	    }


		return $result;
		
	}



	public function executeAllItens( $user_id, $view_id ){
				
		$result=false;
		
		try {
					
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			
			if( !empty($view_id)){
			
				$query = sprintf("SELECT * FROM \"%s%s\" 
									WHERE user_id=%d and view_id=%u
									ORDER BY execute_order asc, id asc;",
									configuration::table_prefix,
				                 	configuration::table_transformation, 
				                 	application::getUser_id(), 
				                 	$view_id);
						
				$result = $db->getQuery($query);
				
				if(count($result)>0){
					
					foreach( $result as $key=>$item )						
						$this->executeItem($user_id, $item->id);
					
				}
				
			}
		
		}
	    catch (AppException $e) {
	    	
			throw new AppException( $e->getMessage() );
			
	    }


		return $result;
		
	}
	
	

	
}